local TexLuaLib = class("testLuaLib")
local json = require("rapidjson")
local pb = require("pb")
local protoc = require("xlua/protoc")

function TexLuaLib:Awake()
    self:TestJson()
    self:TestProbuf()
end

function TexLuaLib:TestJson()
    local t = json.decode('{"a":123}')
    print(t.a)
    t.a = 456
    local s = json.encode(t)
    print("json", s)
end

function TexLuaLib:TestProbuf()
    assert(
        protoc:load [[
        message Phone {
            optional string name        = 1;
            optional int64  phonenumber = 2;
        }
        message Person {
            optional string name     = 1;
            optional int32  age      = 2;
            optional string address  = 3;
            repeated Phone  contacts = 4;
        } ]]
    )

    local data = {
        name = "ilse",
        age = 18,
        contacts = {
            {name = "alice", phonenumber = 12312341234},
            {name = "bob", phonenumber = 45645674567}
        }
    }

    local bytes = assert(pb.encode("Person", data))
    print(pb.tohex(bytes))

    local data2 = assert(pb.decode("Person", bytes))
    print(data2.name)
    print(data2.age)
    print(data2.address)
    print(data2.contacts[1].name)
    print(data2.contacts[1].phonenumber)
    print(data2.contacts[2].name)
    print(data2.contacts[2].phonenumber)

end

return TexLuaLib
